package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;

import controller.ActionList;
import controller.StartSimulation;
import controller.StartSimulationMulti;

import model.BinForm;

/**
 * 
 * @author Estelle && Marie
 *
 * This class is the main window of our program
 * 
 */


public class MainWindow {
	/**
	 * - window is the principal element of our window
	 * - form is the panel where we can fill each JTextFied (present in a BinForm) to create a new simulation
	 * - binform contains each JTextField
	 * - centermain contains the result of the simulation 
	 * - menuStart contains the two button : start with thread and start without it
	 * - left contain the list of simulations
	 * - progressbar allows to the user to see the evolution of the simulation
	 * - contentPane is a container where we find the JPanel centermain and left
	 */
	private JFrame window = new JFrame();
	private JPanel form= new JPanel();
	private BinForm binform = new BinForm();
	private JPanel centermain = new JPanel();
	private JPanel menuStart = new JPanel();
	private JPanel left = new JPanel();
	private ProgressBar progress = new ProgressBar();
	private Container contentPane = window.getContentPane();	
	    
	
	public MainWindow(){

		/**
		 * In first time, we initialise the sytle of the window
		 */
		
		this.window.setTitle("MONTECARLO MIAMI");
		this.window.setSize(600,360);
		this.window.setLocationRelativeTo(null);
		
		this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * we arange each element in the contentPane thank to the springlayout
		 */
	    SpringLayout layout = new SpringLayout();
		this.window.setLayout(layout);
		contentPane.setBackground(Color.magenta);
		
		layout.putConstraint(SpringLayout.WEST, left, 5, SpringLayout.WEST, contentPane);
	    layout.putConstraint(SpringLayout.NORTH, left, 10 , SpringLayout.NORTH, contentPane);
	    layout.putConstraint(SpringLayout.NORTH, centermain, 10, SpringLayout.NORTH, contentPane);
	    layout.putConstraint(SpringLayout.WEST, centermain, 5, SpringLayout.EAST, left);
	    
//--------------------------------------------------------------------------------------------------------------------------------------------------------		
		/**
		 *  Initialization of each panel in the window
		 */
	    
		centermain.setSize(new Dimension(400,400));
		centermain.setLayout(new BorderLayout());
		centermain.setBackground(Color.magenta);
		
		left.setSize(new Dimension(100,400));
		left.setLayout(new BorderLayout());
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------
		/**
		 *  Initialization of the elements in the contentPane
		 */
		//form where is on the left of the contentPane
		form.setBackground(Color.MAGENTA);
		form.setVisible(true);
		form.setLayout(new GridLayout(9,1));
		
		//evolution where is on the centermain of the contentPane
		JTextPane evolution = new JTextPane();
		evolution.setBackground(Color.pink);
		evolution.setText("You can see here, the evolution of the program");
		//evolution.setForeground(Color.WHITE);
		
	    //creation of startButton in the menu start
	    JButton startButton = new JButton("START WITHOUT THREADS");
	    startButton.setBounds(50, 60, 80, 30);
	           
	    startButton.setToolTipText("to start the simulation withou thread");
	    startButton.setBackground(Color.PINK);
	    startButton.setForeground(Color.magenta);
	    	
	    //add the action listener of startButton
	    startButton.addActionListener(new StartSimulation(binform,evolution,this.progress));
	    
	    //creation of startmultiButton in the menu start
	    JButton startmultiButton = new JButton("START MULTI-THREADS");
	    startmultiButton.setBounds(50, 60, 80, 30);
	    
	    startmultiButton.setBackground(Color.PINK);
	    startmultiButton.setForeground(Color.magenta);
	    
	    startmultiButton.setToolTipText("to start the simulation multithread");
	  
	    //add the action listener of startmultiButton  
	    startmultiButton.addActionListener(new StartSimulationMulti(binform,evolution, this.getProgress()));
	    
//--------------------------------------------------------------------------------------------------------------------------------------------------------
		
	    /**
	     * Initialization of the list where is out of the contentPane
	     */
	    
	    //creation startmultiButton
	    JList list = new JList();
	    StringBuilder buff = new StringBuilder();
	    String[] text = new String[10];
	    
	    	//here we create the string with each name of the list which represents a example of simulations
	    for(int i=0; i<10 ; i++){
	    	buff.append("list").append((i+1));
	    	text[i] = buff.toString();
	    	buff.setLength(0);
	    	
	    }
	 
	    list.setListData(text);
	    list.setBackground(Color.magenta);
	    list.setForeground(Color.white);
	    list.addListSelectionListener(new ActionList(list, evolution, binform,this.getProgress()));
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------
		/**
		 * Set the elements in each panel, in the contentpane, and in the window.....
		 */
	    // we use a function in the class binform to add each JTextField to the form
	    binform.addToPannel(form);
	    
	    progress.setVisible(true);
	    
	    
	    menuStart.add(startButton);
	    menuStart.add(startmultiButton);
	    menuStart.add(progress);
	    
	    left.add(list, BorderLayout.CENTER);
	    
	    centermain.add(evolution, BorderLayout.CENTER);
	    centermain.add(form, BorderLayout.EAST);	    
	    centermain.add(menuStart, BorderLayout.SOUTH);

	    window.add(left);
	    window.add(centermain);    
		this.window.setVisible(true);
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------

	/**
	 * geter and seter for the progress bar
	 * 
	 */
	
	public ProgressBar getProgress() {
		return progress;
	}


	public void setProgress(ProgressBar progress) {
		this.progress = progress;
	}

}